package com.bachelor.community.controller;

import com.bachelor.community.model.entity.Answer;
import com.bachelor.community.model.entity.Comment;
import com.bachelor.community.model.entity.Vote;
import com.bachelor.community.service.intf.AnswerService;
import com.bachelor.community.service.intf.CommentService;
import com.bachelor.community.service.intf.VoteService;
import com.bachelor.community.utils.Result;
import com.bachelor.community.utils.UserSupport;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = {"/answers/{answerId}/voters", "/comments/{commentId}/voters"})
public class VoteController {
    @Resource
    private VoteService voteService;
    @Resource
    private AnswerService answerService;
    @Resource
    private CommentService commentService;
    @Resource
    private UserSupport userSupport;

    @PostMapping
    public Result insertVote(@PathVariable(value = "answerId", required = false) Integer answerId,
                             @PathVariable(value = "commentId", required = false) Integer commentId,
                             @RequestBody Vote vote) {
        Integer voter = userSupport.getCurrentUserId();
        vote.setVoter(voter);
        voteService.save(vote);
        if (vote.getEntityType().equals(1)) {
            Answer answer = answerService.getById(answerId);
            answer.setVoteCount(answer.getVoteCount() + 1);
            answerService.updateById(answer);
        } else {
            Comment comment = commentService.getById(commentId);
            comment.setLikeCount(comment.getLikeCount() + 1);
            commentService.updateById(comment);
        }
        return Result.succeed("201", vote.getId());
    }

    @GetMapping
    public Result checkVote(@PathVariable(required = false) Integer answerId,
                            @PathVariable(required = false) Integer commentId) {
        Integer voter = userSupport.getCurrentUserId();
        if (answerId != null && commentId == null) {
            return Result.succeed("200", voteService.checkUserVoteState(voter, answerId, 1));
        } else {
            return Result.succeed("200", voteService.checkUserVoteState(voter, commentId, 2));
        }
    }

    @DeleteMapping("{voteId}")
    public Result deleteVote(@PathVariable Integer voteId,
                             @PathVariable(required = false) Integer answerId,
                             @PathVariable(required = false) Integer commentId) {
        if (answerId != null && commentId == null) {
            Answer answer = answerService.getById(answerId);
            answer.setVoteCount(answer.getVoteCount() - 1);
            answerService.updateById(answer);
        } else {
            Comment comment = commentService.getById(commentId);
            comment.setLikeCount(comment.getLikeCount() - 1);
            commentService.updateById(comment);
        }
        return Result.succeed("204", voteService.removeById(voteId));
    }

    @PatchMapping("{voteId}")
    public Result updateVote(@PathVariable Integer voteId,
                             @PathVariable(required = false) Integer answerId,
                             @PathVariable(required = false) Integer commentId) {
        Vote vote = voteService.getById(voteId);
        if (answerId != null && commentId == null) {
            Answer answer = answerService.getById(answerId);
            if (vote.getAttitude().equals("approval")) {
                vote.setAttitude("disapproval");
                answer.setVoteCount(answer.getVoteCount() - 1);
            } else {
                vote.setAttitude("approval");
                answer.setVoteCount(answer.getVoteCount() + 1);
            }
            answerService.updateById(answer);
        } else {
            Comment comment = commentService.getById(commentId);
            if (vote.getAttitude().equals("approval")) {
                vote.setAttitude("neutral");
                comment.setLikeCount(comment.getLikeCount() - 1);
            } else {
                vote.setAttitude("approval");
                comment.setLikeCount(comment.getLikeCount() + 1);
            }
        }
        return Result.succeed("200", voteService.updateById(vote));
    }
}
